package me.hbz.myblog.doman.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBUtils {

	// 1. 创建私有的属性*（连接数据库必要的四个变量）：driver url user password
	private static String DRIVER = "com.mysql.jdbc.Driver";//
	private static String URL = "jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8";
	private static String USER = "root";
	private static String PASSWORD = "root";

	// 2. 将构造函数私有化
	private DBUtils() {

	}

	// 3.将注册驱动写入静态代码块
	// 其中静态代码块只执行一次。构造代码块在每次创建对象是都会执行。
	static {
		// 注册驱动
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(URL, USER, PASSWORD);
	}

	public static void closeResource(Connection conn, Statement st, ResultSet rs) throws SQLException {
		rs.close();
		st.close();
		conn.close();
	}

	public static void closeResource(Connection conn, Statement st) throws SQLException {
		st.close();
		conn.close();
	}
}
